package com.defysoft.waukids.slide;

import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukids.BaseActivity;
import com.defysoft.waukids.Data_Sharedpreferences;
import com.defysoft.waukids.PublicSharedpreferences;
import com.defysoft.waukids.R;
import com.defysoft.waukids.Sqlite_Connect;

public class Main_Slide_Info_Yacguan extends BaseActivity {
	Toolbar toolbar;
	TextView title, mainTextOne, mainTextTwo;
	String focus;
	PublicSharedpreferences psf;
	Data_Sharedpreferences.SF_Member member;
	public Sqlite_Connect sqlite_Connect;
	private final int YACGUAN_THREAD = 1;
	private final int SUCCESS_YACGUAN = 2;
	private final int FAIL_YACGUAN = 3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_slide_info_yacguan);
		setStartData();
		setStartToolbar();
		startNetwork(YACGUAN_THREAD);

		registerReceiver(mToastMessageReceiver, new IntentFilter(
				"com.defysoft.waukids.TOAST_MESSAGE"));
	}

	@Override
	public void setStartData() {
		super.setStartData();
		psf = new PublicSharedpreferences(this);
		psf.setMembersp();
		member = psf.getMembersp();
		sqlite_Connect = new Sqlite_Connect(this);
		focus = getIntent().getStringExtra("INFO_FOCUS");

		toolbar = (Toolbar) findViewById(R.id.MAIN_SLIDE_INFO_YACGUAN_TOOLBAR);
		title = (TextView) findViewById(R.id.MAIN_SLIDE_INFO_YACGUAN_TITLE);
		mainTextOne = (TextView) findViewById(R.id.MAIN_SLIDE_INFO_YACGUAN_TEXTVIEW_ONE);
	}

	@Override
	public void startThread(int choice) {
		super.startThread(choice);
		final int cho = choice;
		firstThread = new Thread(new Runnable() {
			@Override
			public void run() {
				switch (cho) {
				case YACGUAN_THREAD:
					yacguanThread();
					break;
				}
			}
		});
		startDIal();
		firstThread.start();
	}

	protected void yacguanThread() {
		String msg = "";
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_MAIN_JOIN_YACGUAN));
			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(SUCCESS_YACGUAN,
						jsonObject.getString("payload"));
			} else {
				onNetworkCallback(FAIL_YACGUAN, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString()+" where yacguanThread()");
		}
	}

	@Override
	protected void handlingMessage(Message _message) {
		super.handlingMessage(_message);
		switch (_message.what) {
		case SUCCESS_YACGUAN:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			JSONObject yacguan;
			try {
				yacguan = new JSONObject((String) _message.obj);
				if (!(focus.equals("") || focus.equals(null))) {
					if (focus.equals("FIRST")) {
						title.setText(yacguan.getString("First_Name"));
						mainTextOne.setText(yacguan.getString("First_Sub"));
					} else if (focus.equals("SECOND")) {
						title.setText(yacguan.getString("Two_Name"));
						mainTextOne.setText(yacguan.getString("Two_Sub"));
					} else if (focus.equals("THIRD")) {
						title.setText(yacguan.getString("Three_Name"));
						mainTextOne.setText(yacguan.getString("Three_Sub"));

					}
				}

			} catch (Exception e) {
			}

			break;

		case FAIL_YACGUAN:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			Toast.makeText(this, (String) _message.obj, Toast.LENGTH_SHORT)
					.show();
			break;

		case CATCH_EXCEPTION:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			sqlite_Connect.insert_Err_Log((String)_message.obj,android.os.Build.VERSION.SDK_INT,"Main_Slide_Info_Yacguan", member.Email);
			break;
		}
	}

	private void setStartToolbar() {
		toolbar.setNavigationOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private final BroadcastReceiver mToastMessageReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			LinearLayout layout = (LinearLayout)getLayoutInflater().inflate(R.layout.toast_layout, null);
			TextView toast_tv = (TextView)layout.findViewById(R.id.TOAST_LAYOUT_TV);
			Toast toast = new Toast(Main_Slide_Info_Yacguan.this);
			toast.setView(layout);
			toast_tv.setText(intent.getStringExtra("PUSH_NAME")+" : "+intent.getStringExtra("PUSH_SUB"));
			toast.setDuration(Toast.LENGTH_LONG);
			toast.show();
		}
	};
}
